package com.silverhaze.diamond.common.entity;

import com.silverhaze.commons.db.datasource.ObjectEntity;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * © Ivan Babanin (06.07.2007 20:00:08)
 * This part of code is strictly confidential.
 * © Magnificent Software Inc.
 */
@Entity()
@Table(name = "customers")
public class Customer extends ObjectEntity {
    private static final long serialVersionUID = 2689127L;

    @Basic()
    @Column(name = "name")
    private String name;

    @Basic()
    @Column(name = "patronymic")
    private String patronymic;

    @Basic()
    @Column(name = "surname")
    private String surname;

    @Basic()
    @Column(name = "organization")
    private String organization;

    @Basic()
    @Column(name = "bOrganization")
    private boolean bOrganization;

    @Basic()
    @Column(name = "phone")
    private String phone;

    @Basic()
    @Column(name = "address")
    private String address;

    @Basic()
    @Column(name = "email")
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPatronymic() {
        return patronymic;
    }

    public void setPatronymic(String patronymic) {
        this.patronymic = patronymic;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    @Transient()
    public String getExtendedName() {
        if (bOrganization) {
            return organization;
        } else {
            return getSurname() + " " + getName() + " " + getPatronymic();
        }
    }

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    public boolean isBOrganization() {
        return bOrganization;
    }

    public void setBOrganization(boolean bOrganization) {
        this.bOrganization = bOrganization;
    }

    @Transient()
    public List<Service> getServices() {
        List<Service> result = new ArrayList<Service>();

//        for (Service s : DAOLocator.getServices()) {
//            if (s.getOwner().equals(this)) {
//                result.add(s);
//            }
//        }

        return result;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Customer customer = (Customer) o;

        if (address != null ? !address.equals(customer.address) : customer.address != null) return false;
        if (phone != null ? !phone.equals(customer.phone) : customer.phone != null) return false;

        if (bOrganization) {
            if (organization != null ? !organization.equals(customer.organization) : customer.organization != null)
                return false;
        } else {
            if (name != null ? !name.equals(customer.name) : customer.name != null) return false;
            if (patronymic != null ? !patronymic.equals(customer.patronymic) : customer.patronymic != null)
                return false;
            if (surname != null ? !surname.equals(customer.surname) : customer.surname != null) return false;
        }

        return true;
    }

    public int hashCode() {
        int result;
        result = (name != null ? name.hashCode() : 0);
        result = 31 * result + (patronymic != null ? patronymic.hashCode() : 0);
        result = 31 * result + (surname != null ? surname.hashCode() : 0);
        result = 31 * result + (phone != null ? phone.hashCode() : 0);
        result = 31 * result + (address != null ? address.hashCode() : 0);
        result = 31 * result + (organization != null ? organization.hashCode() : 0);
        return result;
    }
}

